package com.aiyige.model.commontag.util;

import android.text.TextUtils;

import com.aiyige.model.commontag.CommonTag;
import com.aiyige.page.interest.model.Interest;
import com.aiyige.utils.ListUtil;

import java.util.LinkedList;
import java.util.List;

public class CommonTagUtil {
    public static boolean checkEqual(List<CommonTag> sourceList, List<CommonTag> destList) {
        if (ListUtil.isEmpty(sourceList) && ListUtil.isEmpty(destList)) return true;
        if (ListUtil.isEmpty(sourceList) || ListUtil.isEmpty(destList)) return false;
        if (sourceList.size() != destList.size()) return false;
        outLoop:
        for (CommonTag src : sourceList) {
            for (CommonTag dest : destList) {
                if (src.getId() == null && dest.getId() == null) continue outLoop;
                if (src.getId() == null || dest.getId() == null) continue;
                if (src.getId().equals(dest.getId())) continue outLoop;
            }
            return false;
        }
        return true;
    }

    public static List<String> extractIdList(List<CommonTag> commonTagList) {
        List<String> idList = new LinkedList<>();
        for (CommonTag commonTag : commonTagList) {
            if (TextUtils.isEmpty(commonTag.getId())) continue;
            idList.add(commonTag.getId());
        }
        return idList;
    }


    public static void clearSelect(List<CommonTag> targetList) {
        for (CommonTag CommonTag : targetList) {
            CommonTag.setSelected(false);
        }
    }

    public static int countOfSelected(List<CommonTag> commonTagList) {
        if (ListUtil.isEmpty(commonTagList)) return 0;
        int count = 0;
        for (CommonTag commonTag : commonTagList) {
            if (commonTag.isSelected()) count++;
        }
        return count;
    }

    public static List<CommonTag> extractSelected(List<CommonTag> commonTagList) {
        List<CommonTag> selectedList = new LinkedList<>();
        for (CommonTag commonTag : commonTagList) {
            if (commonTag.isSelected()) {
                selectedList.add(new CommonTag(commonTag));
            }
        }
        return selectedList;
    }

    public static void resumeSelect(List<CommonTag> targetList, List<CommonTag> selectedList) {
        if (ListUtil.isEmpty(selectedList)) return;
        if (ListUtil.isEmpty(targetList)) return;
        outLoop:
        for (CommonTag selectedItem : selectedList) {
            if (selectedItem.getId() == null) continue;
            for (CommonTag targetItem : targetList) {
                if (targetItem.getId() == null) continue;
                if (targetItem.getId().equals(selectedItem.getId())) {
                    targetItem.setSelected(true);
                    continue outLoop;
                }
            }
        }
    }
}
